package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.OnPlayerTurnStartSubscriber;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.actions.watcher.ChooseOneAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.rooms.MonsterRoom;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.cards.optionCards.ChooseMonsterCard;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.powers.TauntPower;
import demoMod.invincibleOne.relics.TaskGoal;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Aggression extends AbstractInvincibleCard implements OnPlayerTurnStartSubscriber {
    public static final String ID = InvincibleOneMod.makeID("Aggression");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Aggression.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.NONE;
    private static final int COST = -2;

    public Aggression() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        this.baseMagicNumber = this.magicNumber = 10;
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public void onRemoveFromMasterDeck() {
        super.onRemoveFromMasterDeck();
        BaseMod.unsubscribeLater(this);
    }

    @Override
    public boolean onBattleStart() {
        BaseMod.unsubscribe(this);
        BaseMod.subscribe(this);
        return true;
    }

    @Override
    public void receiveOnPlayerTurnStart() {
        if (TaskGoal.combatTurn == 0) {
            addToBot(new AbstractGameAction() {
                @Override
                public void update() {
                    List<AbstractMonster> aliveMonsters = AbstractDungeon.getMonsters().monsters.stream().filter(monster -> !monster.isDeadOrEscaped()).collect(Collectors.toList());
                    if (aliveMonsters.size() >= 2) {
                        ArrayList<AbstractCard> options = getAbstractCards(aliveMonsters);
                        addToBot(new ChooseOneAction(options));
                    } else if (!aliveMonsters.isEmpty()) {
                        AbstractMonster m = aliveMonsters.get(0);
                        addToBot(new ApplyPowerAction(m, m, new TauntPower(m)));
                        if (m.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                            addToBot(new GainBlockAction(m, m, magicNumber * 2));
                        } else {
                            addToBot(new GainBlockAction(m, m, magicNumber));
                        }
                    }
                    this.isDone = true;
                }

                private ArrayList<AbstractCard> getAbstractCards(List<AbstractMonster> aliveMonsters) {
                    ArrayList<AbstractCard> options = new ArrayList<>();
                    for (AbstractMonster monster : aliveMonsters) {
                        AbstractCard option = new ChooseMonsterCard(monster, () -> {
                            addToBot(new ApplyPowerAction(monster, monster, new TauntPower(monster)));
                            if (monster.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                                addToBot(new GainBlockAction(monster, monster, magicNumber * 2));
                            } else {
                                addToBot(new GainBlockAction(monster, monster, magicNumber));
                            }
                        });
                        options.add(option);
                    }
                    return options;
                }
            });
        }
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(4);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }
}
